//
//  SafeChangePasswordController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/4.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "SafeChangePasswordController.h"
#import "LMCountDownButton.h"
@interface SafeChangePasswordController ()
@property (weak, nonatomic) IBOutlet LMCountDownButton *codeButton;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@end

@implementation SafeChangePasswordController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.leftbutton setImage:imgname(@"lefticon") forState:(UIControlStateNormal)];
    
    self.phoneTextField.text = self.phoneStr;
    
    LMCountDownButton *sendCodeButton = [[LMCountDownButton alloc] init];
    [sendCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [sendCodeButton setTitleColor:[UIColor colorWithHexString:@"#FF7321"] forState:(UIControlStateNormal)];
    sendCodeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    sendCodeButton.frame = self.codeButton.bounds;
    [sendCodeButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.codeButton addSubview:sendCodeButton];

}
// 获取验证码
-(void)click:(LMCountDownButton *)sender{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //去除字符串中空格
    self.phoneTextField.text = [self.phoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (![self isMobileNumberOnly:self.phoneTextField.text]) {
        ShowToastWithText(@"请输入正确电话号码");
        return;
    }
    NSDictionary *dic = @{@"phone":self.phoneTextField.text};
    
    [NetWorkTool POST:sendSms param:[dic mutableCopy] success:^(id dic) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [sender startCountDown];
        ShowToastWithText(@"验证码发送成功");
    } other:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:NO];
}
- (IBAction)confirmButtonClick:(UIButton *)sender {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"phone"] = self.phoneTextField.text;
    param[@"code"] = self.codeTextField.text;
    param[@"password"] = self.passwordTextField.text;
    //去除字符串中空格
    self.phoneTextField.text = [self.phoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (![self isMobileNumberOnly:self.phoneTextField.text]) {
        ShowToastWithText(@"请输入正确的手机号");
        return;
    }
    
    if ([self.codeTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"验证码不能为空");
        return;
    }
    if ([self.passwordTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入新密码");
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:forgetPassword param:param success:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    } other:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        
    } needUser:NO];
}
///判断是否为电话号码
- (BOOL)isMobileNumberOnly:(NSString *)mobileNum {
    NSString * MOBILE = @"^((10[0-9])|(11[0-9])|(12[0-9])|(13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    if ([regextestmobile evaluateWithObject:mobileNum] == YES) {
        return YES;
    } else {
        return NO;
    }
}
@end
